$(function() {

	addColumn() ;
	initOnblur();
});

function addColumn() {
	
	/**
	 * 新增
	 */
	$('.add-column-item').on('click',function() {
		var itemTable = $('.data-tr');
		var len = itemTable.find('tr').length - 1;
		var sort = len+1 ;
		var htmlList ='<tr>';
        htmlList += '<td><input name="cloumn['+ len+'].name" class="form-control column_name" type="text" required></td>';
        htmlList += '<td><input name="cloumn['+ len+'].comments" class="form-control " type="text" required></td>';
        htmlList += '<td style="position: relative;">';
        htmlList += '    <select name="cloumn['+ len+'].jdbcType" class="form-control editable_select jdbc_type" required>';
        htmlList += '    	<option value="">--请选择--</option>';
		htmlList += '		<option value="bigint(18)" >bigint(18)</option>';
		htmlList += '		<option value="varchar(32)" >varchar(32)</option>';
		htmlList += '		<option value="varchar(64)" >varchar(64)</option>';
		htmlList += '		<option value="int(11)" >int(11)</option>';
		htmlList += '		<option value="double(10)" >double(10)</option>';
		htmlList += '		<option value="decimal(10,2)" >decimal(10,2)</option>';
		htmlList += '		<option value="datetime" >datetime</option>';
		htmlList += '		<option value="text" >text</option>';
		htmlList += '		<option value="longtext" >longtext</option>';
        htmlList += '    </select>';
        htmlList += '</td>';
        htmlList += '<td><input name="cloumn['+ len+'].jdbcLength" class="form-control jdbcLen" type="text" ></td>';
        htmlList += '<td>';
        htmlList += '	<select name="cloumn['+ len+'].javaType" class="form-control attr_type" required>';
        htmlList += '		<option value="">--请选择--</option>';
		htmlList += '			<option value="String" >String</option>';
		htmlList += '		<option value="Long"  >Long</option>';
		htmlList += '		<option value="Integer" >Integer</option>';
		htmlList += '		<option value="Double" >Double</option>';
		htmlList += '		<option value="Date"   >Date</option>';
        htmlList += '	</select>';
        htmlList += '</td>';
        htmlList += '<td><input name="cloumn['+ len+'].javaField" class="form-control attr_name" type="text" required></td>';
        htmlList += '<td><label class="check-box table-check-box-center"><input name="cloumn['+ len+'].isPk" type="checkbox" value="1"></label></td>';
        
        htmlList += '<td><input name="cloumn['+ len+'].sort" class="form-control table-form-control_w_50" type="text" value="'+(len+1)+'" required></td>';
		htmlList +='<td>';
		htmlList +='	<a href="javascript:;" onclick="$(this).parents(\'tr\').remove();">删除</a>';
		htmlList +='</td>';
		
		htmlList +='</tr>';
		$(".data-tr tr:last").after(htmlList);
		EditableSelectView.init() ;
		initOnblur();
		initChk();
	});
	
}

function initChk() {
	$(".check-box").iCheck({
    	checkboxClass: 'icheckbox-blue',
		radioClass: 'iradio-blue',
    })
}

function initOnblur() {
	$('.column_name').blur(function(){
		var colName = $(this).val() ;
		var attrName = transformat(colName) ;
		$(this).parents('tr').find('.attr_name').val(attrName);
	}) ;
	
	$('.jdbc_type').blur(function(){
		
		var jdbcType = $(this).val() ;
		if(jdbcType.indexOf('varchar')>= 0) {
			$(this).parents('tr').find('.attr_type').find('option[value="String"]').attr("selected", true);
		}
		if(jdbcType.indexOf('text')>= 0) {
			$(this).parents('tr').find('.attr_type').find('option[value="String"]').attr("selected", true);
		}
		if(jdbcType.indexOf('datetime')>= 0) {
			$(this).parents('tr').find('.attr_type').find('option[value="Date"]').attr("selected", true);
		}
		if(jdbcType.indexOf('bigint')>= 0) {
			$(this).parents('tr').find('.attr_type').find('option[value="Long"]').attr("selected", true);
		}
		if(jdbcType.indexOf('bigint')< 0 && jdbcType.indexOf('int')>= 0) {
			$(this).parents('tr').find('.attr_type').find('option[value="Integer"]').attr("selected", true);
		}
		if(jdbcType.lastIndexOf(')') >0) {
			var len = jdbcType.substring(jdbcType.lastIndexOf('(')+1,jdbcType.lastIndexOf(')')) ;
			$(this).parents('tr').find('.jdbcLen').val(len) ;
		}
		
	}) ;
}

function transformat(param){
	  var arr=param.split("_");
	  for(var i=1;i<arr.length;i++){
	    arr[i]=arr[i].charAt(0).toUpperCase()+arr[i].substring(1);
	  }
	  return arr.join("");
}


 